//
//  TGSubtitlesView.m
//  GroupBuy
//
//  Created by wwj on 15/11/5.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGSubtitlesView.h"
#import "UIImage+Extend.h"
#import "TGSubitleItem.h"
#define kTitleW 102
#define kTitleH 40

#define kDuration 0.3
#define kAlpha 1

@interface TGSubtitlesView ()
@property (weak, nonatomic) TGSubitleItem *titleItemBtn;
@end
@implementation TGSubtitlesView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.image = [UIImage resizedImage:@"bg_subfilter_other.png"];
        self.userInteractionEnabled = YES;
        self.clipsToBounds = YES;
    }
    return self;
}

-(void)setMainTile:(NSString *)mainTile{
    _mainTile = mainTile;
}

-(void)setTitleArray:(NSArray *)titleArray{
    
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:kAllStr];
    [array addObjectsFromArray:titleArray];
    _titleArray = array;
    
    for (int index = 0; index < _titleArray.count; index++) {
        TGSubitleItem *btn;
        if (index >= self.subviews.count) {
            btn = [[TGSubitleItem alloc] init];
            [self addSubview:btn];
        }else{
            btn = self.subviews[index];
        }
        btn.hidden = NO;
        [btn setTitle:_titleArray[index] forState:UIControlStateNormal];
        if (_getTitleBlock) {
            NSString *title = _getTitleBlock();
            
            if ([_mainTile isEqualToString:title] && index == 0) {
                btn.selected = YES;
                _titleItemBtn = btn;
            }else{
                if ([_titleArray[index] isEqualToString:title]) {
                    btn.selected = YES;
                    _titleItemBtn = btn;
                }else{
                    btn.selected = NO;
                    _titleItemBtn = nil;
                }
            }
        }
       
        
        [btn addTarget:self action:@selector(onClickSubTitleBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    for (int index = titleArray.count; index < self.subviews.count; index++) {
        TGSubitleItem *btn = self.subviews[index];
        btn.hidden = YES;
    }
    [self layoutSubviews];
}

#pragma -mark 子标题点击事件
- (void)onClickSubTitleBtn:(TGSubitleItem *)titleItemBtn{
    _titleItemBtn.selected = NO;
    titleItemBtn.selected = YES;
    _titleItemBtn = titleItemBtn;
    if (_setTitleBlock) {
        NSString *title =[titleItemBtn titleForState:UIControlStateNormal];
        
        if ([kAllStr isEqualToString:title]) {
            title = _mainTile;
        }
        _setTitleBlock(title);
    }
}

-(void)layoutSubviews{
    int columns = self.frame.size.width / kTitleW;
    for (int index = 0; index<_titleArray.count; index++) {
        TGSubitleItem *btn = self.subviews[index];
        
        CGFloat X = index % columns * kTitleW;
        CGFloat Y = index / columns * kTitleH;
        btn.frame = CGRectMake(X, Y, kTitleW, kTitleH);
    }
    [UIView animateWithDuration:kDuration animations:^{
        CGRect frame = self.frame;
        int row = (_titleArray.count + columns - 1) / columns;
        frame.size.height = row * kTitleH;
        self.frame = frame;
    }];
    [super layoutSubviews];
}

-(void)show{
    [self layoutSubviews];
    self.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
    self.alpha = 0;
    [UIView animateWithDuration:kDuration animations:^{
        self.transform = CGAffineTransformIdentity;
        self.alpha = kAlpha;
    }];
}

-(void)hidden{
    [UIView animateWithDuration:kDuration animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, -self.frame.size.height);
        self.alpha = kAlpha;
    } completion:^(BOOL finished) {
        self.transform = CGAffineTransformIdentity;
        self.alpha = 0;
        [self removeFromSuperview];
    }];
}

@end
